CFLAGS = -g -O0 -Wall -Wshadow -Wfloat-equal -ftrapv
LDFLAGS =
CC = gcc
# -fverbose-asm -save-temps
PROJ_CFLAGS = $(shell getconf LFS_CFLAGS) -std=gnu99 -D_GNU_SOURCE ${CFLAGS}
PROJ_LDFLAGS = $(shell getconf LFS_LDFLAGS) -lrt ${LDFLAGS}
FUSE_CFLAGS = $(shell pkg-config --cflags fuse) -DFUSE_USE_VERSION=28
FUSE_LDFLAGS = $(shell pkg-config --libs fuse)
BINDIR = .
TMPDIR = tmp
OUTDIRS = $(BINDIR) $(TMPDIR)
LIBCPFS_OBJS := $(addprefix $(TMPDIR)/,cpfs.o log.o)
EXES = $(addprefix $(BINDIR)/,mount.cpfs mkfs.cpfs)

all: $(EXES)

$(BINDIR)/mount.cpfs: mount.cpfs.c cpfsfuse.c libcpfs.a
	$(CC) -o $@ $^ $(FUSE_CFLAGS) $(PROJ_CFLAGS) $(FUSE_LDFLAGS) $(PROJ_LDFLAGS)

$(BINDIR)/mkfs.cpfs: mkfs.cpfs.c libcpfs.a
	$(CC) -o $@ $^ $(PROJ_CFLAGS) $(PROJ_LDFLAGS)

$(TMPDIR)/cpfs.o: cpfs.c $(TMPDIR)
	$(CC) -o $@ -c $^ $(PROJ_CFLAGS)

$(TMPDIR)/log.o: log.c
	$(CC) -o $@ -c $^ $(PROJ_CFLAGS)

$(EXES) : | $(BINDIR)
$(LIBCPFS_OBJS) : | $(TMPDIR)
$(OUTDIRS):
	@test -d $@ || mkdir $@

libcpfs.a: $(LIBCPFS_OBJS)
	ar rcs $@ $^
